<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();
        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');

       // $this->output->enable_profiler(TRUE);
        //Load Model
        $this->load->Model("admin/Mbook");
        $this->load->Model("admin/Mimage");
        $this->load->Model("admin/Mauthor");
        $this->load->Model("admin/Mcategory");
        $this->load->Model("admin/Mcategory_book");
        $this->load->Model("admin/Mchapter");
        $this->load->Model("admin/Mvolume");
        $this->load->Model("admin/Mpromotion");
    }

    // process for chapter table
    public function checkChapterNo()
    {
        $no = $this->input->get('no');
        $book_id = $this->input->get('bookID');
        $volume_no = $this->input->get('volume');

        if($volume_no != 0){
            $listChapter = $this->Mchapter->getChapterByBookIDByVolumeNo($book_id, $volume_no);
        }else{
            $listChapter = $this->Mchapter->getChapterByBookID($book_id);
        }

        $ketqua = '0';
        foreach ($listChapter as $chapter){
            if($no == $chapter['no']){
                $ketqua = '1';
                break;
            }
        }
        echo $ketqua;
    }

    public function checkChapterUname()
    {
        $name = $this->input->get('name');
        echo mb_strtolower(url_title(removesign($name)));
    }

    public function updateChapterList()
    {
        if($this->input->post('id')){
            $data['id'] = $this->input->post('id');
            $data['no'] = $this->input->post('chapterNo');
            $data['name'] = $this->input->post('chapterName');
            $data['uname'] = $this->input->post('chapterUname');
            $data['book_id'] = $this->input->post('chapterBook');
            
            if($this->input->post('chapterVolume') == 0){
                $volume = NULL;
            }else{
                $volume = $this->input->post('chapterVolume');
            }
            $data['volume_no'] = $volume;
            
            $data['open'] = $this->input->post('chapterOpen');
            
            if($this->input->post('chapterNextNo') == 0){
                $next_no = NULL;
            }else{
                $next_no = $this->input->post('chapterNextNo');
            }
            $data['next_no'] = $next_no;
            
            if($this->input->post('chapterPrevNo') == 0){
                $prev_no = NULL;
            }else{
                $prev_no = $this->input->post('chapterPrevNo');
            }
            $data['prev_no'] = $prev_no;

            $this->Mchapter->updateChapterAjax($data); 
        }
    }
    
	public function getVolumeByBookID()
    {     
        if($this->input->post('bookID')){
        	$book_id = $this->input->post('bookID');
        	header('Content-Type: application/x-json; charset=utf-8');
            echo (json_encode($this->Mvolume->getVolumeByBookID($book_id)));
        }
    }

    // process for category table
    public function checkCategoryUname()
    {
        $name = $this->input->get('name');
        echo mb_strtolower(url_title(removesign($name)));
    }

    public function checkCategoryUniqueName()
    {
        $uname = $this->input->get('uname');

        $listCategory = $this->Mcategory->getCategoryAll();

        $ketqua = '0';
        foreach ($listCategory as $category){
            if($uname == $category['uname']){
                $ketqua = '1';
                break;
            }
        }
        echo $ketqua;
    }

    // process for book table
    public function checkBookUname()
    {
        $name = $this->input->get('name');
        echo mb_strtolower(url_title(removesign($name)));
        exit;
    }

    public function checkBookUniqueNameByCategoryID()
    {
        $uname = $this->input->get('uname');
        $categoryID = $this->input->get('categoryID');
        
        $listBook = $this->Mbook->getBooksByCategoryID($categoryID);
        
        $ketqua = '0';
        foreach ($listBook as $book){
            if($uname == $book['uname']){
                $ketqua = '1';
                break;
            }
        }
        echo $ketqua;
        exit;
    }
    
   // Table Promotion
   
    public function updatePromotionStatus()
    {
        if($this->input->post('id')){
            $data['id'] = $this->input->post('id');
            $data['status'] = $this->input->post('status');
            echo "<pre>";
                print_r($data);
            echo "</pre>";
            $this->Mpromotion->updatePromotionStatus($data); 
        }
    } 

}